﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace InventoryManager
{
    public partial class ViewInvoiceForm_2_UserControl : UserControl
    {
        public event FormChangedHandler FormChangedEvent;

        private string _connectionString;

        private int _selectedRowIndex;
        private int _selectedColumnIndex;
        private string _invoiceID;
        private string _invoiceDate;

        public ViewInvoiceForm_2_UserControl(string connectionString, object[] param, int width, int height)
        {
            InitializeComponent();

            _connectionString = connectionString;

            this.Width = width;
            this.Height = height;

            _selectedRowIndex = -1;
            _selectedColumnIndex = -1;
            _invoiceID = "";
            _invoiceDate = "";

            SearchResultDataGridView.DataSource = (DataTable)param[0];
            TitleLabel.Text = "ผลการค้นหา จากวันที่ " + (string)param[1] + " ถึงวันที่ " + (string)param[2];
        }

        private void OpenReport(int invoiceID)
        {
            TaxDeliveryReceiptSelectionForm taxDeliveryReceiptSelectionForm
                = new TaxDeliveryReceiptSelectionForm();
            ShowReportForm showReportForm = new ShowReportForm(_connectionString);

            switch (taxDeliveryReceiptSelectionForm.ShowDialog())
            {
                case DialogResult.OK:
                    string branchName;
                    if (taxDeliveryReceiptSelectionForm.MainCompanyRadioButton.Checked)
                        branchName = "สำนักงานใหญ่";
                    else
                        branchName = taxDeliveryReceiptSelectionForm.BranchNameTextBox.Text;

                    if (taxDeliveryReceiptSelectionForm.TaxDeliveryOrigRadioButton.Checked)
                        showReportForm.setReportToShow(3, invoiceID, true, true, branchName);
                    else if (taxDeliveryReceiptSelectionForm.TaxDeliveryCopyRadioButton.Checked)
                        showReportForm.setReportToShow(3, invoiceID, false, true, branchName);
                    else if (taxDeliveryReceiptSelectionForm.ReceiptOrigRadioButton.Checked)
                        showReportForm.setReportToShow(4, invoiceID, true, true, branchName);
                    else
                        showReportForm.setReportToShow(4, invoiceID, false, true, branchName);

                    showReportForm.LoadReport();
                    showReportForm.ShowDialog();
                    break;
                default:
                    break;
            }
        }

        private void SearchResultDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                DataGridViewCell clickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[0];

                ShowReportForm showReportForm = new ShowReportForm(_connectionString);
                string invoiceID = clickedCell.Value.ToString();
                OpenReport(Int32.Parse(invoiceID));
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            FormChangedEvent("INVOICE_VIEW_1", null);
        }

        private void SearchResultDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _selectedRowIndex = e.RowIndex;
            _selectedColumnIndex = e.ColumnIndex;

            if (_selectedRowIndex != -1 && _selectedColumnIndex != -1)
            {
                if (e.Button == MouseButtons.Right)
                {
                    DataGridViewCell clickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[_selectedColumnIndex];

                    // Here you can do whatever you want with the cell
                    this.SearchResultDataGridView.CurrentCell = clickedCell;  // Select the clicked cell, for instance

                    // Get mouse position relative to the vehicles grid
                    var relativeMousePosition = SearchResultDataGridView.PointToClient(Cursor.Position);

                    // Show the context menu and set ID
                    DataGridViewCell pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[0];
                    _invoiceID = pickedCell.Value.ToString();
                    pickedCell = ((DataGridView)sender).Rows[_selectedRowIndex].Cells[2];
                    _invoiceDate = pickedCell.Value.ToString();
                    RightClickMenu.Show(SearchResultDataGridView, relativeMousePosition);
                }
            }
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenReport(Int32.Parse(_invoiceID));
        }

        private void EditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRequisitionAndInvoiceForm editRequisitionAndInvoiceForm = 
                new EditRequisitionAndInvoiceForm(_connectionString, 3, Int32.Parse(_invoiceID));
            editRequisitionAndInvoiceForm.EditFormClosedEvent += new EditFormClosedHandler(editRequisitionAndInvoiceForm_EditFormClosedEvent);
            editRequisitionAndInvoiceForm.ShowDialog();
        }

        void editRequisitionAndInvoiceForm_EditFormClosedEvent(int recordType)
        {
            if (recordType == 3)
                FormChangedEvent("INVOICE_SELECTION", null);
        }
    }
}
